import { d as defineComponent, s as storeToRefs, k as onMounted, r as ref, o as openBlock, c as createElementBlock, f as createBaseVNode, b as createVNode, w as withCtx, m as withKeys, a as resolveComponent, h as unref, t as toDisplayString, q as withDirectives, v as vShow, g as createBlock, F as Fragment, e as renderList, x as createCommentVNode, i as createTextVNode, E as ElMessage, n as search_default, _ as _export_sfc } from "./index-NZguwuNX.js";
import { u as useCounterStore, h as http } from "./index-F6B3vg3q.js";
import { _ as _sfc_main$1 } from "./Pagination.vue_vue_type_script_setup_true_lang-x-Hoizt3.js";
const _hoisted_1 = { class: "agent" };
const _hoisted_2 = { class: "df jc-sb title-btn" };
const _hoisted_3 = ["onDblclick"];
const _hoisted_4 = ["onDblclick"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "codePass",
  setup(__props) {
    const store = useCounterStore();
    const { userInfo } = storeToRefs(store);
    onMounted(async () => {
      let total_arr = await get_list("SELECT COUNT(*) FROM cami", "userglobal");
      total.value = Number(total_arr[0]["COUNT(*)"]);
      await get_list("SELECT * FROM cami ORDER BY cami.IndexID LIMIT 15 OFFSET 0", "userglobal");
      console.log(tableData.value);
    });
    const page = ref(1);
    const pageSize = ref(15);
    const total = ref(10);
    const handleSizeChange = async (val) => {
      console.log(`${val} items per page`);
      pageSize.value = val;
      if ((page.value - 1) * pageSize.value - 1 <= 0) {
        await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET 0`, "userglobal");
      } else {
        await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET ${(page.value - 1) * pageSize.value}`, "userglobal");
      }
    };
    const handleCurrentChange = async (val) => {
      console.log(`current page: ${val}`);
      page.value = val;
      if ((val - 1) * pageSize.value - 1 <= 0) {
        await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET 0`, "userglobal");
      } else {
        await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET ${(page.value - 1) * pageSize.value}`, "userglobal");
      }
    };
    const search_inp = ref("");
    const search_click = async () => {
      if (search_inp.value == "") {
        await get_list("SELECT * FROM cami ORDER BY cami.IndexID LIMIT 15 OFFSET 0", "userglobal");
      } else
        await get_list(
          `SELECT * FROM cami WHERE IndexID LIKE '%${search_inp.value}%' OR Distributor LIKE  '%${search_inp.value}%' OR CamiID LIKE '%${search_inp.value}%' OR AgentUserName LIKE '%${search_inp.value}%' OR OwnerUserName LIKE '%${search_inp.value}%' OR CamiUseTime LIKE '%${search_inp.value}%'`,
          "userglobal"
        );
    };
    const daili_list = ref([]);
    const daili_model = ref("");
    const db_click = async (row) => {
      daili_model.value = "";
      daili_list.value = await get_list_daili("SELECT * FROM agentuser", "userglobal");
      console.log(daili_model.value);
      tableData.value.forEach((item) => {
        item["status"] = false;
      });
      console.log(row);
      row["status"] = true;
    };
    const daili_blur = async (row) => {
      console.log(row);
      console.log(daili_model.value);
      row["status"] = false;
      if (daili_model.value == "") {
        return false;
      }
      const res = await http.corsApi("/", "post", {
        packID: "123456",
        packageType: "adminSql",
        packageContent: {
          AgentUserName: userInfo.value.username,
          AgentUserPassword: userInfo.value.password,
          sql: `UPDATE cami SET AgentUserName = '${daili_model.value}'  WHERE IndexID = '${row.IndexID}'`,
          dbName: "userglobal"
        }
      });
      console.log(res);
      if (res && res.packageContent.packageStatus === 0) {
        ElMessage({
          message: "更新成功",
          type: "success"
        });
        if ((page.value - 1) * pageSize.value - 1 <= 0) {
          await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET 0`, "userglobal");
        } else {
          await get_list(`SELECT * FROM cami ORDER BY cami.IndexID LIMIT ${pageSize.value} OFFSET ${(page.value - 1) * pageSize.value}`, "userglobal");
        }
      } else {
        ElMessage({
          message: "更新失败",
          type: "error"
        });
      }
    };
    const recycle = async (row) => {
      const res = await http.corsApi("/", "post", {
        packID: "123456",
        packageType: "adminSql",
        packageContent: {
          AgentUserName: userInfo.value.username,
          AgentUserPassword: userInfo.value.password,
          sql: `UPDATE cami SET AgentUserName = null  WHERE IndexID = '${row.IndexID}'`,
          dbName: "userglobal"
        }
      });
      console.log(res);
      if (res && res.packageContent.packageStatus === 0) {
        ElMessage({
          message: "收回成功",
          type: "success"
        });
        await get_list("SELECT * FROM cami ORDER BY cami.IndexID LIMIT 15 OFFSET 0", "userglobal");
      } else {
        ElMessage({
          message: "收回失败",
          type: "error"
        });
      }
    };
    const table_click = () => {
      console.log(111);
    };
    const tableData = ref([]);
    const get_list = async (sql, dbName) => {
      console.log(userInfo.value);
      const res = await http.corsApi("/", "post", {
        packID: "123456",
        packageType: "adminSql",
        packageContent: {
          AgentUserName: userInfo.value.username,
          AgentUserPassword: userInfo.value.password,
          sql,
          dbName
        }
      });
      let result = [];
      if (res.packageContent.packageStatus === 0 && res.packageContent.packageMsg !== "成功") {
        result = JSON.parse(res.packageContent.packageMsg);
      } else {
        result = [];
      }
      tableData.value = result;
      console.log(tableData.value);
      return result;
    };
    const get_list_daili = async (sql, dbName) => {
      console.log(userInfo.value);
      const res = await http.corsApi("/", "post", {
        packID: "123456",
        packageType: "adminSql",
        packageContent: {
          AgentUserName: userInfo.value.username,
          AgentUserPassword: userInfo.value.password,
          sql,
          dbName
        }
      });
      let result = [];
      if (res.packageContent.packageStatus === 0 && res.packageContent.packageMsg !== "成功") {
        result = JSON.parse(res.packageContent.packageMsg);
      } else {
        result = [];
      }
      return result;
    };
    return (_ctx, _cache) => {
      const _component_el_button = resolveComponent("el-button");
      const _component_el_input = resolveComponent("el-input");
      const _component_el_table_column = resolveComponent("el-table-column");
      const _component_el_option = resolveComponent("el-option");
      const _component_el_select = resolveComponent("el-select");
      const _component_el_popconfirm = resolveComponent("el-popconfirm");
      const _component_el_table = resolveComponent("el-table");
      return openBlock(), createElementBlock("div", _hoisted_1, [
        createBaseVNode("div", _hoisted_2, [
          createBaseVNode("div", null, [
            createVNode(_component_el_input, {
              modelValue: search_inp.value,
              "onUpdate:modelValue": _cache[0] || (_cache[0] = ($event) => search_inp.value = $event),
              onKeydown: withKeys(search_click, ["enter"]),
              placeholder: "输入任意字段进行模糊查询",
              class: "input-with-select"
            }, {
              append: withCtx(() => [
                createVNode(_component_el_button, {
                  onClick: search_click,
                  icon: unref(search_default)
                }, null, 8, ["icon"])
              ]),
              _: 1
            }, 8, ["modelValue"])
          ])
        ]),
        createVNode(_component_el_table, {
          data: tableData.value,
          stripe: "",
          border: "",
          style: { "width": "100%", "height": "650px", "margin-top": "15px" },
          onClick: table_click
        }, {
          default: withCtx(() => [
            createVNode(_component_el_table_column, {
              prop: "IndexID",
              label: "索引",
              width: "150"
            }),
            createVNode(_component_el_table_column, {
              prop: "AgentUserName",
              label: "卡密所有者",
              width: "150"
            }, {
              default: withCtx((scope) => [
                scope.row.AgentUserName && !scope.row.status ? (openBlock(), createElementBlock("div", {
                  key: 0,
                  onDblclick: ($event) => db_click(scope.row)
                }, toDisplayString(scope.row.AgentUserName), 41, _hoisted_3)) : withDirectives((openBlock(), createElementBlock("div", {
                  key: 1,
                  style: { "color": "#ccc" },
                  onDblclick: ($event) => db_click(scope.row)
                }, "双击进行编辑", 40, _hoisted_4)), [
                  [vShow, !scope.row.status]
                ]),
                scope.row.status ? (openBlock(), createBlock(_component_el_select, {
                  key: 2,
                  modelValue: daili_model.value,
                  "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => daili_model.value = $event),
                  filterable: "",
                  placeholder: "请选择代理",
                  onBlur: ($event) => daili_blur(scope.row)
                }, {
                  default: withCtx(() => [
                    (openBlock(true), createElementBlock(Fragment, null, renderList(daili_list.value, (item) => {
                      return openBlock(), createBlock(_component_el_option, {
                        key: item.AgentUserName,
                        label: item.AgentUserName,
                        value: item.AgentUserName
                      }, null, 8, ["label", "value"]);
                    }), 128))
                  ]),
                  _: 2
                }, 1032, ["modelValue", "onBlur"])) : createCommentVNode("", true)
              ]),
              _: 1
            }),
            createVNode(_component_el_table_column, {
              prop: "CamiBalance",
              label: "卡密余额",
              width: "100"
            }),
            createVNode(_component_el_table_column, {
              prop: "CamiCreateTime",
              label: "卡密创建时间",
              width: "180"
            }),
            createVNode(_component_el_table_column, {
              prop: "CamiID",
              label: "卡密号",
              width: "300"
            }),
            createVNode(_component_el_table_column, {
              prop: "CamiUseTime",
              label: "卡密使用时间",
              width: "180"
            }),
            createVNode(_component_el_table_column, {
              prop: "Distributor",
              label: "卡密被分配者"
            }),
            createVNode(_component_el_table_column, {
              prop: "OwnerUserName",
              label: "卡密使用者"
            }),
            createVNode(_component_el_table_column, {
              prop: "",
              label: "操作",
              width: "300"
            }, {
              default: withCtx((scope) => [
                createVNode(_component_el_popconfirm, {
                  title: "确定要收回吗?",
                  "confirm-button-text": "确定",
                  "cancel-button-text": "取消",
                  onConfirm: ($event) => recycle(scope.row)
                }, {
                  reference: withCtx(() => [
                    createVNode(_component_el_button, null, {
                      default: withCtx(() => [
                        createTextVNode("收回")
                      ]),
                      _: 1
                    })
                  ]),
                  _: 2
                }, 1032, ["onConfirm"])
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["data"]),
        createVNode(_sfc_main$1, {
          style: { "margin-top": "10px" },
          total: total.value,
          onHandleSizeChange: handleSizeChange,
          onHandleCurrentChange: handleCurrentChange
        }, null, 8, ["total"])
      ]);
    };
  }
});
const codePass = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-dfa77044"]]);
export {
  codePass as default
};
